package com.songlanyun.modules.publicbenefitmsg.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.core.type.TypeReference;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.DonationConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.donationpoints.entity.DonationPointsEntity;
import com.songlanyun.modules.message.model.*;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.publicbenefitmsg.model.BuyMsg;
import com.songlanyun.modules.publicbenefitmsg.model.DonateMsg;
import com.songlanyun.modules.publicbenefitmsg.model.IPublicBenefitMsgBody;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 公益消息
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-05 11:06:31
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("yxt_public_benefit_msg")
public class PublicBenefitMsgEntity extends RootEntity<Long, PublicBenefitMsgEntity> implements Serializable {
    private static final long serialVersionUID = 1L;

    public PublicBenefitMsgEntity(){}

    public PublicBenefitMsgEntity(Long userId, DonationConstant.MsgType type, IPublicBenefitMsgBody msgBody, String content) {
        this.userId = userId;
        this.type = type;
        this.msg = ClassConvertUtils.transformJSONToSnake(msgBody);
        this.content = content;
    }

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 内容
     */
    private String content;
    /**
     * 消息类型
     */
    private DonationConstant.MsgType type;
    /**
     * 消息
     */
    private String msg;

    /**
     * 消息体
     */
    @TableField(exist = false)
    private IPublicBenefitMsgBody msgBody;

    @SuppressWarnings("all")
    public IPublicBenefitMsgBody loadMsgBody() {
        try {
            switch (type) {
                case DONATE:
                    this.msgBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<DonateMsg>() {
                    });
                    break;
                case BUY:
                    this.msgBody = ClassConvertUtils.stringToClassSnake(this.msg, new TypeReference<BuyMsg>() {
                    });
                    break;
                default:
                    return null;
            }
            return this.msgBody;
        } catch (Exception e) {
            throw new RRException("消息内容转换失败");
        }
    }

    private Long userId;
    /**
     *
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @Override
    public Long getIdentity() {
        return this.getId();
    }
}
